<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>遍历商品</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }

    .nav {
        width: 200px;
        height: auto;
        margin: 200px;
        background-color: skyblue;
    }

    .nav ul>li {
        height: 50px;
        line-height: 50px;
        background-color: #8aadbc;
        position: relative;
    }

    .nav ol {
        width: auto;
        max-width: 600px;
        height: 500px;
        background-color: antiquewhite;
        position: absolute;
        top: 200px;
        left: 400px;
        display: none;
    }

    .nav ol>li {
        float: left;
        width: 50%;
    }
</style>

<body>
    <div class="nav">
        <ul>
            <!-- <li>全部商品
            </li>
            <li>手机
            </li>
            <li>耳机
            </li>
            <li>洗衣机</li>
            <li>冰箱</li>
            <li>红米</li>
            <li>空调</li>
            <li>笔记本</li> -->
        </ul>
        <ol>

        </ol>
    </div>
</body>
<script src="js/jquery-3.6.4.min.js"></script>
<script src="js/xiaomi.js"></script>
<script>
    $(function () {
        for (var i = 0; i < menuarr.length; i++) {
            var li = document.createElement('li')
            li.setAttribute('index', i)



            li.onmouseover = function () {
                li.parentNode.nextElementSibling.style.display = 'block'
                var idx = this.getAttribute('index')
                console.log(idx);

                for (var k in data[idx]) {
                    for (var m = 0; m < data[idx][k].length; m++) {
                        var li1 = document.createElement('li')
                        var a = document.createElement('a')
                        var img = document.createElement('img')
                        var arr = [];
                        for (var n in data[idx][k][m]) {
                            arr.push(data[idx][k][m][n])
                        }
                        a.innerText = arr[1]
                        img.src = arr[0]
                        li1.append(a)
                        li1.append(img)
                        $('.nav>ol').append(li1);
                    }
                }

            }




            li.innerHTML = menuarr[i]
            $('.nav>ul').append(li);



            li.onmouseout = function () {
                var lis = document.querySelectorAll('.nav>ol>li')
                for (var i = 0; i < lis.length; i++) {
                    $('.nav>ol')[0].removeChild(lis[i])
                }
                li.parentNode.nextElementSibling.style.display = 'none'
            }
        }



    })
</script>

</html>